/**
 * Created by zander on 2017/11/24.
 */

function userConsole() {
    var localStorage = window.localStorage;
    var name = localStorage.getItem('name');
    $.ajax({
        type: "POST",
        url: "/getRunningJob",
        data: {username: name},
        dataType: "json",
        success: function (ret) {
            if (ret.status == "0000") {
                var jobdata = ret['content'];
                for (var i in jobdata) {
                    var jobName = jobdata[i]['jobname'];
                    var buildId = jobdata[i]['build_id'];
                    var sp = '<li><a onclick="getconsole(this)" build_id="' + buildId + '">' + jobName + '</a></li>';
                    $("#xzul").append(sp);
                }
            } else {
                alert(ret.message);
            }
        },
        error: function () {
            alert("Error 500,请联系开发人员！");
        }
    });
}

function getconsole(e) {
    var jobname = $(e).text();
    var build_id = $(e).attr('build_id');
    logRefresh(jobname, build_id);
}

function logRefresh(jobname, build_id) {
    $.ajax({
        url: "/getconsole",
        type: "POST",
        data: {"build_id": build_id, "jobname": jobname},
        dataType: 'json',
        success: function (req) {
            if (req.status = "0000") {
                $("#console").innerHTML = "";
                document.getElementById("console").innerHTML = req['console']
            } else {
                alert("日志查询失败,请稍后重试！");
            }
        },
        error: function () {
            alert("Error 500,请联系开发人员！");
        }
    });
}

window.onload = userConsole();